package com.wisely.highlight_spring4.ch3.aware;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

/**
 * @ClassName AwareService
 * @Description TODO
 * @Author Mike
 * @Date 2018/11/25 19:05
 * @Version 1.0
 */
@Service
public class AwareService implements BeanNameAware,ResourceLoaderAware {

    private String beanName;
    private ResourceLoader loader;

    @Override
    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.loader = resourceLoader;
    }

    public void outputResult() {
        System.out.println("Bean 的名称为：" + beanName);

        Resource resource = loader.getResource("classpath:com/wisely/highlight_spring4/ch3/aware/test.txt");

        try{
            System.out.println("ResourceLoader 加载的文件内容为： " + IOUtils.toString(resource.getInputStream()));
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
